import { getAttendence, getHelmetSign, getNotLeave, getProjectInfo } from '@/api/home'

const state = {
  attendanceToday: '', // 今日出工人数
  worker: '', // 总人数,
  helmetSign: '', // 未戴安全帽的警告人数
  notLeaveNum: '', // 超时未离场人员
  projectInfo: {}// 工程信息详情
}

const mutations = {
  /**
   *修改attendanceToday
   */
  SET_ATTENDANCE: (state, data) => {
    state.attendanceToday = data
  },
  SET_WORKER: (state, data) => {
    state.worker = data
  },
  SET_HELMET_SIGN: (state, data) => {
    state.helmetSign = data
  },
  SET_NOT_LEAVE: (state, data) => {
    state.notLeaveNum = data === null ? 0 : data
  },
  SET_PROJECT_INFO: (state, data) => {
    state.projectInfo = data
  }
}

const actions = {
  /**
   * 获取今日出工人数
   */
  getAttendence({ commit }) {
    return new Promise((resolve, reject) => {
      getAttendence().then(res => {
        commit('SET_ATTENDANCE', res.data.attendanceToday)
        commit('SET_WORKER', res.data.worker)
      }).catch(err => {
        reject(err)
      })
    })
  },

  /**
   * 获取未戴安全帽警示人数
   */
  getHelmetSign({ commit }) {
    return new Promise((resolve, reject) => {
      getHelmetSign().then(res => {
        commit('SET_HELMET_SIGN', res.data.todayCount)
      }).catch(err => {
        reject(err)
      })
    })
  },

  /**
   * 获取超时未离开人数
   */
  getMotLeave({ commit }) {
    return new Promise((resolve, reject) => {
      getNotLeave().then(res => {
        commit('SET_NOT_LEAVE', res.data.count)
      }).catch(err => {
        reject(err)
      })
    })
  },

  /**
   * 获取工程信息
   * @params{number|string} projectsId -- 工程id
   */
  getProjectInfo({ commit }, projectsId) {
    return new Promise((resolve, reject) => {
      getProjectInfo(projectsId).then(res => {
        commit('SET_PROJECT_INFO', res.data)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
